<template>
    <div>
        <NavBar title="批量调度" :leftArrow="true" :clickLeft="() => $router.go(-1)"></NavBar>
        <PilotSearch />
        <List />
    </div>
</template>

<script>
import NavBar from '@/components/NavBar'
import PilotSearch from '@/components/task/detail/pilot/Search'
import List from '../../../../components/task/detail/pilot/List'
import { UserPilot, TaskPilotList } from '@/api/task'
import { Toast } from 'vant'
export default {
    components: {
        NavBar,
        PilotSearch,
        List
    },
    data(){
        return {
            id: undefined,
            search: {
                province: undefined,
                city: undefined,
                area: undefined
            },
            pagination: {
                pageNo: 1,
                pageSize: 10,
                total: 0,
                loading: false,
                finished: false
            },
            list: [],
            check: {
                id: undefined
            },
            pilotList: []
        }
    },
    methods: {
        async getList(){
            Toast.loading({
                message: '加载中...',
                forbidClick: true,
            });
            let { search, pagination } = this,
                { data: { data, totalCount } } = await UserPilot({ ...search, ...pagination }),
                { pageNo } = pagination
            if(pageNo === 1){
                this.list = data || [];
            }else{
                this.list = this.list.concat(data || [])
            }
            this.setCheck()
            this.pagination.total = totalCount
            Toast.clear()
        },
        async getTaskPilotList() {
            let { data: { data } } = await TaskPilotList({ taskId: this.id });
            this.pilotList = data || [];
        },
        setCheck(){
            // let { data: { data: list } } = await UserPilot({ pageNo: 1, pageSize: 100 })
            // console.log(this.pilotList)
            // console.log(this.list)
            this.pilotList.forEach(item => {
                this.list.forEach((subItem, index) => {
                    if(item.userId === subItem.userId){
                        subItem.checked = true
                        console.log(subItem)
                        this.list.splice(index, subItem)
                        if(item.isMain === 1){
                            this.check.id = item.userId
                        }
                    }
                })
            })
            
        },
        setDefaultCheck(){
            let checkedList = this.list.filter(item => item.checked)
            if(!checkedList.length && this.list.length){
                this.list[0].checked = true
                this.check.id = this.list[0].userId
                this.list.splice(0, this.list[0])
            }
        }
    },
    async created(){
        this.id = this.$route.params.id
        await this.getTaskPilotList()
        await this.getList()
        this.setDefaultCheck()
    },
    provide(){
        return {
            Search: () => this.search,
            Pagination: () => this.pagination,
            GetList: this.getList,
            List: () => this.list,
            Check: () => this.check
        }
    }
}
</script>

<style>

</style>